package com.test.demo;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;

import org.junit.Test;


public class JDBCutil {
	
	public static void main(String[] args) {
		Connection conn = null;
		Statement statement = null;
		PreparedStatement preparedStatement = null;
		try {
			long startTime = System.currentTimeMillis();
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/book3?useUnicode=true&characterEncoding=UTF-8";
			String user = "root";
			String password = "123456";
			conn = DriverManager.getConnection(url, user, password);
			//静态sql
			statement = conn.createStatement();
//			StringBuilder builder = new StringBuilder();
//			builder.append(" INSERT INTO USER VALUES ");
//			for (int i = 0; i < 100; i++) {
//				if(i==99){
//					builder.append(" ("+i+",'姓名"+i+"')");					
//				}else{
//					builder.append(" ("+i+",'姓名"+i+"'),");
//				}
//			}
//			builder.append(" ;");
//			int row = statement.executeUpdate(builder.toString());
			
			//动态sql
			for (int i = 0; i < 100000; i++) {
				preparedStatement = conn.prepareStatement(" INSERT INTO BOOKS (name,author,date) VALUES (?,?,?); ");
				preparedStatement.setString(1, "原生姓名"+i);
				preparedStatement.setString(2, "原生作者"+i);
				preparedStatement.setTimestamp(3, new Timestamp(new java.util.Date().getTime()));
				int rows = preparedStatement.executeUpdate();
			}
			long endTime = System.currentTimeMillis();
			System.out.println("耗时:"+(new BigDecimal(endTime-startTime)).divide(new BigDecimal(1000L)));
			//查询sql
//		    ResultSet rs = statement.executeQuery(" SELECT id ,NAME FROM user ;");
//			while (rs.next()) {
//				int id = rs.getInt(1);
//				String name = rs.getString(2);
//				System.out.println("id:"+id+"====name:"+name);
//			}
//			rs.close();
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally{
			try {
				statement.close();
				preparedStatement.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
